package cn.gov.chinamor.zxing.util;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class DataEncoder {

	private final static String m_iv = "t8s2k7p2";
	private final static String m_key = "9x3p6r1m";
	
	public static String desDecrypt(String encryptedString) throws Exception {
		
		byte[] byteMi = Base64.decode(encryptedString, Base64.DEFAULT);
		byte[] iv = m_iv.getBytes("ASCII");
		IvParameterSpec zeroIv = new IvParameterSpec(iv);
		SecretKeySpec key = new SecretKeySpec(m_key.getBytes("ASCII"), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
		byte decryptedData[] = cipher.doFinal(byteMi);
		String oriString = new String(decryptedData, "UTF-8");
		
		return oriString;
	}
	
	public static String desEncrypt(String oString) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(m_iv.getBytes("ASCII"));
		SecretKeySpec key = new SecretKeySpec(m_key.getBytes("ASCII"), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
		byte encryptedData[] = cipher.doFinal(oString.getBytes("UTF-8"));
		String encryptedString = Base64.encodeToString(encryptedData, Base64.DEFAULT);
		return encryptedString;
	}
}
